package com.homework;

import java.util.Scanner;

public class ReservationSystem {
    public static void main(String[] args) {
        //定义菜品
        String[] foodNames = {"清蒸鲈鱼","香菇青菜","鱼香肉丝"};//菜名
        double[] price = {30.0,14.0,18.0};//单价
        int[] praise = new int[3];//点赞
        //定义订单
        String[] names = new String[10];
        String[] orderInformation = new String[10];//菜品名称和份数
        int[] time = new int[10];//时间
        String[] site = new String[10];//地址
        double[]  totalAmount = new double[10];//总金额
        int[] state = new int[10];//0:已预订 1:已完成
        //初始化1个订单信息
        names[0] = "铁柱";
        orderInformation[0] = "清蒸鲈鱼 1份";
        time[0] = 10;
        site[0] = "黄山路28号";
        totalAmount[0] = 36;//餐费大于50免配送费，否则配送费6元。
        state[0] = 0;


        Scanner input = new Scanner(System.in);
        boolean flag = true;// 标记用户是否退出系统
        int num = -1; // 用户输入0返回主菜单，否则退出系统
        System.out.println("\n欢迎使用“吃货联盟订餐系统”");
        do {
            // 显示系统主菜单
            System.out.println("****************************");
            System.out.println("1、我要订餐");
            System.out.println("2、查看餐袋");
            System.out.println("3、签收订单");
            System.out.println("4、删除订单");
            System.out.println("5、我要点赞");
            System.out.println("6、退出系统");
            System.out.println("****************************");
            System.out.print("请输入数字选择：");
            int choose = input.nextInt();
            flag = true;
            switch (choose) {
                case 1:
                    // 我要订餐
                    System.out.println("***我要订餐***");
                    //判断订单是否未满
                    boolean isAdd = false;
                    for (int i = 0; i <= names.length; i++) {
                        if (names[i] == null) {
                            //订单未满
                            isAdd = true;
                            //订餐
                            System.out.print("请输入订餐人姓名:");
                            String name = input.next();
                            System.out.println("序号\t菜名\t单价");
                            for (int j = 0; j < foodNames.length; j++) {
                                String praises = (praise[j] == 0) ? "" : praise[j] + "赞";
                                System.out.println((j + 1) + "\t" + foodNames[j] + "\t" + price[j] + "\t" + praises);
                            }
                            //判断菜品编号
                            System.out.print("请输入你要点的菜品:");
                            int not = input.nextInt();
                            while (not < 1 || not > foodNames.length) {
                                System.out.print("本店没有这个菜品，请重新选择:");
                                not = input.nextInt();
                            }
                            System.out.print("请输入你需要的份数:");
                            int number = input.nextInt();

                            System.out.print("请输入送餐时间（送餐时间只能在10-20之间）:");
                            int t = input.nextInt();
                            while (t < 10 || t > 20) {
                                System.out.println("输入有误，请输入10-20之间的整数:");
                                t = input.nextInt();
                            }
                            System.out.print("请输入送餐地址:");
                            String sites = input.next();
                            System.out.println("订餐成功！");
                            String food = foodNames[not - 1] + " " + number + "份";
                            System.out.println("你定的是:" + food);
                            System.out.println("送菜时间是:" + t + "点");
                            //餐费 配送费 总计
                            double disPrice = price[not - 1] * number;//餐费
                            double song = (disPrice > 50) ? 0 : 6;
                            double sumPrice = disPrice + song;
                            System.out.println("餐费:" + disPrice + "元,配送费:" + song + "元，总计:" + sumPrice + "元。");
                            names[i] = name;
                            orderInformation[i] = food;
                            time[i] = t;
                            site[i] = sites;
                            totalAmount[i] = sumPrice;
                            break;
                        }
                    }
                    if (!isAdd) {
                        System.out.println("对不起订餐已满。");
                    }
                    break;
                case 2:
                    // 查看餐袋
                    System.out.println("***查看订单***");
                    System.out.println("编号\t订餐人\t菜品\t\t配送时间\t配送地址\t\t订餐金额\t订单状态");
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] != null) {
                            String times = time[i] + "点";
                            String states = (state[i] == 0) ? "已预订" : "已完成";
                            System.out.println((i + 1) + "\t\t" + names[i] + "\t" + orderInformation[i] + "\t" + times + "\t" + site[i] + "\t" + totalAmount[i] + "\t\t" + states);
                        }
                    }
                    break;
                case 3:
                    // 签收定单
                    System.out.println("***签收订单***");
                    boolean signIn = false;
                    System.out.println("请输入你要签收订单编号：");
                    int singNum = input.nextInt();
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] != null && state[i] == 0 && i == singNum - 1) {
                            signIn = true;
                            state[i] = 1;
                            System.out.println("订单签收成功！");
                        } else if (names[i] != null && state[i] == 1 && i == singNum - 1) {
                            signIn = true;
                            System.out.println("你选择的订单已签收，不能再次签收。");
                        }
                    }
                    break;
                case 4:
                    // 删除订单
                    System.out.println("***删除订单***");
                    boolean delete = false;
                    System.out.print("请输入你要删除的订单编号：");
                    int deleteNum = input.nextInt();
                    for (int i=0;i<names.length;i++){
                        if (names[i]!=null&&state[i]==0&&i==deleteNum-1){
                            delete = true;
                            System.out.println("你选择的订单未签收不能删除！");

                        }
                        else if (names[i]!=null&&state[i]==1&&i==deleteNum-1){
                            delete = true;
                            for (int j=i;j<names.length-1;j++){
                                names[j] = names[j+1];
                                orderInformation[j] = orderInformation[j+1];
                                time[j] = time[j+1];
                                site[j] = site[j+1];
                                totalAmount[j] = totalAmount[j+1];
                                state[j] = state[j+1];
                            }
                            names[names.length-1] = null;
                            orderInformation[names.length-1] = null;
                            time[names.length-1] = 0;
                            site[names.length-1] = null;
                            totalAmount[names.length-1] = 0;
                            state[names.length-1] = 0;

                            System.out.println("删除订单成功！");
                        }
                    }
                    if (!delete){
                        System.out.println("你选择的订单不存在！");
                    }
                    break;
                case 5:
                    // 我要点赞
                    System.out.println("***我要点赞***");
                    System.out.println("序号\t菜名\t单价");
                    for (int j = 0; j < foodNames.length; j++) {
                        String praises = (praise[j] == 0) ? "" : praise[j] + "赞";
                        System.out.println((j + 1) + "\t" + foodNames[j] + "\t" + price[j] + "\t" + praises);
                    }

                    System.out.print("请输入你要点赞的菜品序号：");
                    int praiseNum = input.nextInt();
                    while (praiseNum<1||praiseNum>foodNames.length){
                        System.out.print("本店没有这个菜品，无法点赞！请重新输入一个菜品序号：");
                        praiseNum = input.nextInt();
                    }
                    praise[praiseNum-1]++;
                    break;
                case 6:
                    // 退出系统
                    System.out.println("***成功退出系统***");
                    flag = false;
                    System.out.println("谢谢使用本系统，欢迎下次光临！");
                    System.exit(0);// 正常退出程序
                    break;
                default:
                    flag = false;
                    break;
            }
            if (flag) {
                System.out.print("输入0返回主菜单：");
                num = input.nextInt();
            } else {
                break; // 退出整个循环
            }
        } while (num == 0);
        System.out.println("谢谢光临,欢迎下次光临!");
    }
}
